package com.skivingcloud.admin.sys.controller;

import com.mybatisflex.core.paginate.Page;
import com.skivingcloud.admin.security.UserUtil;
import com.skivingcloud.admin.sys.entity.SysDictionary;
import com.skivingcloud.admin.sys.service.SysDictionaryService;
import com.skivingcloud.common.utils.PageParameter;
import com.skivingcloud.common.utils.ReturnT;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

/**
 *  控制层。
 *
 * @author skivingcloud
 * @since 2023-08-28
 */
@Tag(name = "SysDictionaryController", description = "数据字典API")
@RestController
@RequestMapping("/sysDictionary")
public class SysDictionaryController {
    private static final Logger log = LoggerFactory.getLogger(SysDictionaryController.class);
    @Resource
    private SysDictionaryService sysDictionaryService;
    
    /**
     * 新增数据字典
     * @param sysDictionary 数据字典数据
     * @return 新增结果
     */
    @Operation(summary = "新增数据字典", description = "数据字典",
            parameters = {@Parameter(name = "sysDictionary", description = "数据字典参数")})
    @ApiResponse(responseCode = "2xx",description = "新增数据字典")
    @PostMapping("/addDic")
    @PreAuthorize("hasAuthority('sys:dic:add')")
    public ReturnT<String> addDic(@Validated @RequestBody SysDictionary sysDictionary){
        try{
            sysDictionaryService.addDic(sysDictionary, UserUtil.getCurrentUser());
            return ReturnT.ok("新增数据字典成功");
        }catch(Exception e){
            log.error("新增数据字典失败", e);
            return ReturnT.error("新增数据字典失败");
        }
    }
    
    /**
     * 删除数据字典
     * @param ids 字典id
     * @return 删除结果
     */
    @Operation(summary = "删除数据字典", description = "数据字典",
            parameters = {@Parameter(name = "ids", description = "字典id")})
    @ApiResponse(responseCode = "2xx",description = "删除数据字典")
    @PostMapping("/deleteDic")
    @PreAuthorize("hasAuthority('sys:dic:delete')")
    public ReturnT<String> deleteDic(@RequestBody String[] ids){
        try{
            sysDictionaryService.deleteBatch(ids);
            return ReturnT.ok("删除数据字典成功");
        }catch(Exception e){
            log.error("删除数据字典失败", e);
            return ReturnT.error("删除数据字典失败");
        }
    }
    
    /**
     * 修改数据字典
     * @param sysDictionary 菜单数据
     * @return 修改结果
     */
    @Operation(summary = "修改数据字典", description = "数据字典",
            parameters = {@Parameter(name = "sysDictionary", description = "数据字典参数")})
    @ApiResponse(responseCode = "2xx",description = "修改数据字典")
    @PostMapping("/updateDic")
    @PreAuthorize("hasAuthority('sys:dic:update')")
    public ReturnT<String> updateDic(@RequestBody @Validated SysDictionary sysDictionary){
        try{
            sysDictionaryService.updateDic(sysDictionary, UserUtil.getCurrentUser());
            return ReturnT.ok("修改数据字典成功");
        }catch(Exception e){
            log.error("修改数据字典失败", e);
            return ReturnT.error("修改数据字典失败");
        }
    }

    /**
     * 根据主键获取详细信息。
     *
     * @param id 主键
     * @return 详情
     */
    @GetMapping("/getInfo/{id}")
    @Operation(summary="根据id查询字典")
    public ReturnT<SysDictionary> getInfo(@PathVariable(name="id") Serializable id) {
        SysDictionary dictionary = sysDictionaryService.getInfoByIdHasParent(id);
        return ReturnT.ok(dictionary);
    }

    /**
     * 分页查询。
     *
     * @param page 分页对象
     * @return 分页对象
     */
    @GetMapping("page")
    @Operation(summary="分页查询字典")
    public Page<SysDictionary> page(@RequestBody Page<SysDictionary> page) {
        return sysDictionaryService.page(page);
    }
    
    /**
     * 根据字典编号取字典数据
     *
     * @param classCode 字典编码
     * @return 字典项
     */
    @GetMapping("/getDicList/{classCode}")
    @Operation(summary="根据编号获取字典")
    public ReturnT<List<SysDictionary>> getDicList(@PathVariable("classCode") String classCode) {
        List<SysDictionary> list = sysDictionaryService.queryByClassCode(classCode);
        return ReturnT.ok(list);
    }
    
    /**
     * 分页查询数据字典
     * @param parameter 查询参数
     * @return 分页查询结果
     */
    @Operation(summary = "分页查询字典", description = "字典列表",
            parameters = {@Parameter(name = "parameter", description = "分页查询条件")})
    @ApiResponse(responseCode = "2xx",description = "分页字典列表")
    @PostMapping("/queryByPage")
    public ReturnT<Page<SysDictionary>> queryByPage(@RequestBody PageParameter<SysDictionary> parameter){
        Page<SysDictionary> page = sysDictionaryService.queryByPage(parameter);
        return ReturnT.ok(page);
    }
}
